package ink.ykb.snowflake.config;

import java.net.UnknownHostException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

	@Bean("redisTemplate")
	public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory connectionFactory) throws UnknownHostException{
		
		RedisTemplate<String,Object> template = new RedisTemplate<String,Object>();
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		//解决应类加载器不同（RestartClassLoader）导致的转换类型异常
		ClassLoader loader = this.getClass().getClassLoader();
		JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer(loader);
		
		template.setConnectionFactory(connectionFactory);
		template.setDefaultSerializer(jdkSerializer);
		template.setKeySerializer(stringSerializer);
		template.setValueSerializer(jdkSerializer);
		template.setHashKeySerializer(stringSerializer);
		template.setHashValueSerializer(jdkSerializer);
		
		return template;
	}
	
	
	
}
